package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysCommunity;
import org.dromara.system.domain.bo.SysCommunityBo;
import org.dromara.system.domain.vo.SysCommunityVo;
import org.dromara.system.mapper.SysCommunityMapper;
import org.dromara.system.service.ISysCommunityService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 小区Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-20
 */
@RequiredArgsConstructor
@Service
public class SysCommunityServiceImpl implements ISysCommunityService {

    private final SysCommunityMapper baseMapper;

    /**
     * 查询小区
     */
    @Override
    public SysCommunityVo queryById(Long communityId) {
        return baseMapper.selectVoById(communityId);
    }

    /**
     * 查询小区列表
     */
    @Override
    public TableDataInfo<SysCommunityVo> queryPageList(SysCommunityBo bo, PageQuery pageQuery) {
        QueryWrapper<SysCommunity> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getName()), "c.name", bo.getName());
        Page<SysCommunityVo> result = baseMapper.selectOwnVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询小区列表
     */
    @Override
    public List<SysCommunityVo> queryList(SysCommunityBo bo) {
        LambdaQueryWrapper<SysCommunity> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysCommunity> buildQueryWrapper(SysCommunityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysCommunity> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysCommunity::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), SysCommunity::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getCityCode()), SysCommunity::getCityCode, bo.getCityCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTel()), SysCommunity::getTel, bo.getTel());
        lqw.eq(StringUtils.isNotBlank(bo.getExtCommunityId()), SysCommunity::getExtCommunityId, bo.getExtCommunityId());
        lqw.eq(StringUtils.isNotBlank(bo.getLat()), SysCommunity::getLat, bo.getLat());
        lqw.eq(StringUtils.isNotBlank(bo.getLng()), SysCommunity::getLng, bo.getLng());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysCommunity::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增小区
     */
    @Override
    public Boolean insertByBo(SysCommunityBo bo) {
        SysCommunity add = MapstructUtils.convert(bo, SysCommunity.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommunityId(add.getCommunityId());
        }
        return flag;
    }

    /**
     * 修改小区
     */
    @Override
    public Boolean updateByBo(SysCommunityBo bo) {
        SysCommunity update = MapstructUtils.convert(bo, SysCommunity.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysCommunity entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除小区
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
